﻿//using Kingdee.BOS.App.Data;
//using Kingdee.BOS.Core.CommonFilter;
//using Kingdee.BOS.Core.DependencyRules;
//using Kingdee.BOS.Core.DynamicForm;
//using Kingdee.BOS.Core.DynamicForm.PlugIn;
//using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
//using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
//using Kingdee.BOS.Core.Metadata.EntityElement;
//using Kingdee.BOS.Core.Metadata.FieldElement;
//using Kingdee.BOS.Core.Report;
//using Kingdee.BOS.JSON;
//using Kingdee.BOS.Orm.DataEntity;
//using Kingdee.BOS.Util;
//using Newtonsoft.Json;
//using Newtonsoft.Json.Linq;
//using System;
//using System.Collections.Generic;
//using System.ComponentModel;
//using System.Data;
//using System.Linq;
//using System.Text;

//namespace WSDZ.KM.ZH.OutsourceExpenseBlitemReconciliation
//{
//    [Description("委外耗用盘点单委外对账"), HotUpdate]
//    public class OutsourceReconciliation : AbstractDynamicFormPlugIn
//    {
//        public string btype = "委外耗用盘点单委外对账";
//        public string entyKey = "FEntity";
//        public string currentFormId = string.Empty;
//        public override void AfterBindData(EventArgs e)
//        {
//            base.AfterBindData(e);
//            currentFormId = View.BillBusinessInfo.GetForm().Id;
//        }
//        public override void ButtonClick(ButtonClickEventArgs e)
//        {
//            base.ButtonClick(e);
//            //委外对账
//            if (e.Key.EqualsV1("F_RLKX_Buttondz"))
//            {
//                try
//                {
//                    Reconciliation();
//                }
//                catch (Exception ex)
//                {
//                    View.ShowErrMessage(ex.Message);
//                }
//            }
//        }
//        /// <summary>
//        /// 委外对账
//        /// </summary>
//        public void Reconciliation()
//        {
//            List<PickData> pickList = new List<PickData>();
//            List<BalanceOfBegin> balanceList = new List<BalanceOfBegin>();
//            List<BalanceOfBegin> purPriceList = new List<BalanceOfBegin>();
//            List<InventoryData> inventoryList = new List<InventoryData>();
//            string d1 = Model.GetValue("F_RLKX_SDate").ToStringV1();
//            string d2 = Model.GetValue("F_RLKX_EDate").ToStringV1();
//            int orgId = 0;
//            int stockId = 0;
//            int stockMastId = 0;
//            int suppId = 0;
//            if (d1.IsNullOrEmptyOrWhiteSpace() || d2.IsNullOrEmptyOrWhiteSpace())
//            {
//                View.ShowErrMessage("开始、结束日期不可空!!!");
//                return;
//            }
//            DynamicObject orgObj = Model.GetValue("F_RLKX_OrgId") as DynamicObject;
//            if (orgObj != null)
//                orgId = orgObj["Id"].ToStringV1().ToInt32V1();
//            if (orgId == 0)
//            {
//                View.ShowErrMessage("组织不可为空!!!");
//                return;
//            }
//            DynamicObject stockObj = Model.GetValue("F_RLKX_jgsck") as DynamicObject;
//            if (stockObj != null)
//            {
//                stockId = stockObj["Id"].ToStringV1().ToInt32V1();
//                stockMastId= stockObj["msterID"].ToStringV1().ToInt32V1();
//            }
//            if (stockId == 0)
//            {
//                View.ShowErrMessage("仓库不可为空!!!");
//                return;
//            }
//            DynamicObject suppObj = Model.GetValue("F_RLKX_gys") as DynamicObject;
//            if (suppObj != null)
//                suppId = suppObj["Id"].ToStringV1().ToInt32V1();
//            if (suppId == 0)
//            {
//                View.ShowErrMessage("供应商不可为空!!!");
//                return;
//            }
//            Model.DeleteEntryData(entyKey);

//            string date1 = d1.ToDateTime().ToString("yyyy-MM-dd");
//            string date2 = d2.ToDateTime().ToString("yyyy-MM-dd 23:59:59");

//            //获取领料数据
//            {
//                string sql = $@"/*dialect*/ select 
//matId=pickInfo.matId
//,mastId=pickInfo.mastId
//,matNumber=pickInfo.matNumber
//,unitId=pickInfo.unitId
//,wasteRate=pickInfo.wasteRate
//,wasteQty=(ISNULL(pickInfo.pickQty,0)-ISNULL(returnInfo.tlQty,0))*pickInfo.wasteRate  --合理损耗数量
//,wwllQty=ISNULL(pickInfo.pickQty,0)-ISNULL(returnInfo.tlQty,0) --委外领料数量
//from (
//select 
//matId=mat.FMATERIALID
//,mastId=mat.FMASTERID
//,matNumber=mat.FNUMBER
//,unitId=pickEnty.FUNITID
//,wasteRate=ISNULL(mat.F_RLKX_SHRATE,0) --委外损耗率
//,num=ROW_NUMBER() over(partition by mat.FMATERIALID order by mat.FMATERIALID)
//,pickQty=SUM(ISNULL(pickEnty.FACTUALQTY,0)) over(partition by mat.FMATERIALID)
//from T_SUB_PICKMTRL pick
//left join T_SUB_PICKMTRLDATA pickEnty on pick.FID=pickEnty.FID
//left join T_BD_MATERIAL mat on pickEnty.FMATERIALID=mat.FMATERIALID
//--left join T_BD_UNIT unit on pickEnty.FUNITID=unit.FUNITID
//where pick.FDOCUMENTSTATUS='C'
//and pick.FSTOCKORGID={orgId}
//and pick.FSUPPLIERID={suppId}
//and pickEnty.FSTOCKID={stockId}
//and pick.FDATE>='{date1}'
//and pick.FDATE<='{date2}'
//) pickInfo
//left join
//(
//select 
//matId=mat.FMATERIALID
//,matNumber=mat.FNUMBER
//,num=ROW_NUMBER() over(partition by mat.FMATERIALID order by mat.FMATERIALID)
//,tlQty=SUM(ISNULL(tlEnty.FQTY,0)) over(partition by mat.FMATERIALID)
//from T_SUB_RETURNMTRL tl
//left join T_SUB_RETURNMTRLENTRY tlEnty on tl.FID=tlEnty.FID
//left join T_BD_MATERIAL mat on tlEnty.FMATERIALID=mat.FMATERIALID
//where tl.FDOCUMENTSTATUS='C'
//and tl.FSTOCKORGID={orgId}
//and tl.FSUBSUPPLIERID={suppId}
//and tlEnty.FSTOCKID={stockId}
//and tl.FDATE>='{date1}'
//and tl.FDATE<='{date2}'
//)returnInfo on pickInfo.matId=returnInfo.matId and pickInfo.num=returnInfo.num
//where pickInfo.num=1";
//                DynamicObjectCollection datas = DBUtils.ExecuteDynamicObject(Context,sql);
//                if (datas==null&&datas.Count<1)
//                    return;
//                pickList = datas.DynamicObjectCollectionToDataList<PickData>();
//            }
//            List<int> mastIds = pickList.Select(x => x.mastId).ToList();
//            List<int> matIds = pickList.Select(x => x.matId).ToList();
//            //获取期初结存单价
//            {
//                int size = 2000;
//                int count = mastIds.Count <= size ? 1 : mastIds.Count % size > 0 ? mastIds.Count / size + 1 : mastIds.Count / size;
//                for (int i = 0; i < count; i++)
//                {
//                    List<int> mids = mastIds.Skip(size * i).Take(size).ToList();
//                    string sql = $@"/*dialect*/ select * from (
//select 
//mastId=FMASTERID
//,cbprice=(case when sum(FQTY) !=0 then sum(FAMOUNT)/sum(FQTY) else 0 end)
//from V_HS_OUTACCTG a
//join V_HS_Balance b on a.FID=b.FID
//join
//(
//select FORGID,max(FCLOSEDATE) FCLOSEDATE from t_stk_closeprofile where fcategory='HS' group by 
//FORGID 
//) c on a.facctgorgid=c.FORGID and convert(varchar(7),a.FDATE,121)=convert(varchar(7),DATEADD(month,-1,c.FCLOSEDATE),121)
//where a.facctgorgid={orgId}
//and FMASTERID in ({string.Join(",", mids)})
//group by FMASTERID
//)A where cbprice>0";
//                    DynamicObjectCollection datas = DBUtils.ExecuteDynamicObject(Context, sql);
//                    if (datas != null && datas.Count >0)
//                    {
//                        List<BalanceOfBegin>  dList = datas.DynamicObjectCollectionToDataList<BalanceOfBegin>();
//                        balanceList.AddRange(dList);
//                    }
//                }
//            }
//            //获取采购价目表单价
//            {
//                int size = 2000;
//                int count = matIds.Count <= size ? 1 : matIds.Count % size > 0 ? matIds.Count / size + 1 : matIds.Count / size;
//                for (int i = 0; i < count; i++)
//                {
//                    List<int> mids = matIds.Skip(size * i).Take(size).ToList();
//                    string sql = $@"/*dialect*/ select 
//matId
//,price
//from (
//select 
//matId=mat.FMATERIALID
//,price=enty.FPRICE
//,num=ROW_NUMBER() over(partition by mat.FNUMBER order by enty.FEFFECTIVEDATE desc)
//from t_PUR_PriceListEntry enty
//left join t_PUR_PriceList main on enty.FID=main.FID
//left join T_BD_MATERIAL mat on enty.FMATERIALID=mat.FMATERIALID
//where main.FDOCUMENTSTATUS='C'
//and main.FUSEORGID={orgId}
//and ISNULL(main.FFORBIDSTATUS,'A')='A'
//and mat.FMATERIALID in ({string.Join(",", mids)})
//)A where num=1";
//                    DynamicObjectCollection datas = DBUtils.ExecuteDynamicObject(Context, sql);
//                    if (datas != null && datas.Count > 0)
//                    {
//                        List<BalanceOfBegin> dList = datas.DynamicObjectCollectionToDataList<BalanceOfBegin>();
//                        purPriceList.AddRange(dList);
//                    }
//                }
//            }
//            //查询即时库存
//            {
//                int size = 2000;
//                int count = mastIds.Count <= size ? 1 : mastIds.Count % size > 0 ? mastIds.Count / size + 1 : mastIds.Count / size;
//                for (int i = 0; i < count; i++)
//                {
//                    List<int> mids = mastIds.Skip(size * i).Take(size).ToList();
//                    string sql = $@"/*dialect*/ select 
//matId=mat.FMATERIALID
//,qty=SUM(ISNULL(jsck.FBASEQTY,0))
//from T_STK_INVENTORY jsck
//left join T_BD_STOCK stock on jsck.FSTOCKID=stock.FSTOCKID
//left join T_BD_MATERIAL mat on jsck.FMATERIALID=mat.FMATERIALID
//where jsck.FSTOCKORGID=1
//and stock.FMASTERID={stockMastId}
//and mat.FMASTERID in ({string.Join(",", mids)})
//group by mat.FMATERIALID";
//                    DynamicObjectCollection datas = DBUtils.ExecuteDynamicObject(Context, sql);
//                    if (datas != null && datas.Count > 0)
//                    {
//                        List<InventoryData> dList = datas.DynamicObjectCollectionToDataList<InventoryData>();
//                        inventoryList.AddRange(dList);
//                    }
//                }
//            }
//            pickList = pickList.Select(x =>
//            {
//                var inventoryObj = inventoryList.Where(a => a.matId == x.matId).FirstOrDefault();
//                if (inventoryObj!=null)
//                    x.inventoryQty = inventoryObj.qty;
//                var balanceObj = balanceList.Where(a => a.mastId == x.mastId).FirstOrDefault();
//                if (balanceObj != null)
//                    x.inventoryPrice = balanceObj.cbprice;
//                else
//                {
//                    var priceObj = purPriceList.Where(a => a.matId == x.matId).FirstOrDefault();
//                    if (priceObj != null)
//                        x.inventoryPrice = priceObj.price;
//                }
//                return x;
//            }).ToList();

//            FillEntry(pickList);
//        }
//        public void FillEntry(List<PickData> pickList)
//        {
//            if (pickList == null || pickList.Count < 1)
//                return;
//            int seq = 0;
//            foreach (var data in pickList)
//            {
//                Model.CreateNewEntryRow(entyKey);
//                Model.SetItemValueByID("F_RLKX_materialid", data.matId, seq);
//                Model.SetItemValueByID("F_RLKX_UnitID", data.unitId, seq);
//                Model.SetValue("F_RLKX_Qtyhxsh", data.wasteQty,seq);
//                Model.SetValue("F_RLKX_Qtyjs", data.inventoryQty, seq);
//                Model.SetValue("F_RLKX_Price", data.inventoryPrice, seq);
//                //委外领料数量
//                Model.SetValue("F_RLKX_Qtysl", data.wwllQty, seq);

//                Model.SetValue("F_RLKX_scfs", "A", seq);

//                Model.SetValue("F_RLKX_Qtyjgcpd", 0);
//                Model.SetValue("F_RLKX_BillStatuskctz","A");
//                seq++;
//            }
//            View.UpdateView(entyKey);
//        }

//        //public void FillEntry(List<PickData> pickList)
//        //{
//        //    if (pickList == null || pickList.Count < 1)
//        //        return;
//        //    Entity entity = this.Model.BillBusinessInfo.GetEntity(entyKey);
//        //    DynamicObjectCollection rows = this.Model.GetEntityDataObject(entity);
//        //    int seq = 1;
//        //    foreach (var data in pickList)
//        //    {
//        //        DynamicObject row = new DynamicObject(entity.DynamicObjectType);
//        //        entity.SeqDynamicProperty.SetValue(row, seq);
//        //        DynamicObject matObj = UtilityManager.GetDynamicObjectById(Context, currentFormId, "F_RLKX_materialid", data.matId);
//        //        if (matObj != null)
//        //        {
//        //            row["F_RLKX_materialid_Id"] = data.matId;
//        //            row["F_RLKX_materialid"] = matObj;
//        //            //View.InvokeFieldUpdateService("F_RLKX_custno", seq);
//        //        }
//        //        DynamicObject unitObj = UtilityManager.GetDynamicObjectById(Context, currentFormId, "F_RLKX_UnitID", data.unitId);
//        //        if (unitObj != null)
//        //        {
//        //            row["F_RLKX_UnitID_Id"] = data.unitId;
//        //            row["F_RLKX_UnitID"] = unitObj;
//        //        }
//        //        row["F_RLKX_Qtyhxsh"] = data.wasteQty;
//        //        row["F_RLKX_Qtyjs"] = data.inventoryQty;
//        //        row["F_RLKX_Price"] = data.inventoryPrice;
//        //        row["F_RLKX_Qtyjgcpd"] = 0;
//        //        rows.Add(row);
//        //        seq++;
//        //    }
//        //    this.View.UpdateView();
//        //    //View.UpdateView(entyKey);
//        //    //for (int i = 0; i < seq; i++)
//        //    //{
//        //    //    //View.InvokeFieldUpdateService("F_RLKX_Qtyjs", i+1);
//        //    //    //View.InvokeFieldUpdateService("F_RLKX_Qtyjgcpd", i+1);
//        //    //}
//        //}

//    }
//    public class PickData
//    {
//        public int matId { get; set; }
//        public int mastId { get; set; }
//        public string matNumber { get; set; }
//        public int unitId { get; set; }
//        public string unitNumber { get; set; }
//        public decimal wasteRate { get; set; }
//        public decimal wasteQty { get; set; }
//        public decimal inventoryPrice { get; set; }
//        public decimal inventoryQty { get; set; }
//        public decimal wwllQty { get; set; }
//    }
//    public class BalanceOfBegin
//    {
//        public int matId { get; set; }
//        public int mastId { get; set; }
//        public decimal cbprice { get; set; }
//        public decimal price { get; set; }
//    }
//    public class InventoryData
//    {
//        public int matId { get; set; }
//        public decimal qty { get; set; }
//    }
//}
